package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.bean.GoodsBeen;
import cn.xfdzx.android.apps.shop.bean.ShopCarListBeen;
import cn.xfdzx.android.apps.shop.contract.GoodActionCallback;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.ShopRetailPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.Utils;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：
 */
public class ShopCarGoodsAdapter extends RecyclerView.Adapter<ShopCarGoodsAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<ShopCarListBeen.DataBean.ValidBean.GoodsBeanX> list;
    private Context context;
    private ShopRetailPresenter presenter;


    public ShopCarGoodsAdapter(Context context, List<ShopCarListBeen.DataBean.ValidBean.GoodsBeanX> list, ShopRetailPresenter presenter) {
        this.list = list;
        this.context = context;
        this.presenter = presenter;

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shop_car, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder,  int position) {
        holder.tvShopGoodsName.setText(list.get(position).getProductInfo().getStore_name());
        RoundedCorners roundedCorners = new RoundedCorners(40);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
//         RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);

        Glide.with(context).load(list.get(position).getProductInfo().getImage()).apply(options.transform(new CenterCrop(), roundedCorners)).into(holder.ivShopPicture);

        holder.tvShopGoodsPrice.setText(list.get(position).getProductInfo().getPrice());
        if (Double.parseDouble(list.get(position).getProductInfo().getOt_price()) == 0) {
            holder.tvShopGoodsPriceOld.setText("");
        } else {
            holder.tvShopGoodsPriceOld.setText(list.get(position).getProductInfo().getOt_price() + "");
        }

        holder.tvShopGoodsPriceOld.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

        holder.tvGoodsNum.setText(list.get(position).getCart_num() + "");
        if (list.get(position).getCart_num() == 0) {
            holder.tvGoodsMinus.setVisibility(View.INVISIBLE);
            holder.tvGoodsNum.setVisibility(View.INVISIBLE);
        } else {
            holder.tvGoodsMinus.setVisibility(View.VISIBLE);
            holder.tvGoodsNum.setVisibility(View.VISIBLE);
        }
        holder.tvGoodsAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()){
                if (EmptyUtil.isEmpty(APP_TOKEN)){
                    Intent intent = new Intent(context, OneLoginMainActivity.class);
                    context.startActivity(intent);
                    return;
                }
//                list.get(position).setCart_num(list.get(position).getCart_num()+1);
//                holder.tvGoodsMinus.setVisibility(View.VISIBLE);
//                holder.tvGoodsNum.setVisibility(View.VISIBLE);
//                holder.tvGoodsNum.setText(list.get(position).getCart_num() + "");

                presenter.updateCarNum(list.get(position).getId()+"",list.get(position).getCart_num()+1 ,0,position);
            }}
        });
        holder.tvGoodsMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()){
                if (EmptyUtil.isEmpty(APP_TOKEN)){
                    Intent intent = new Intent(context, OneLoginMainActivity.class);
                    context.startActivity(intent);
                    return;
                }
//                list.get(position).setCart_num(list.get(position).getCart_num()-1);
                if (list.get(position).getCart_num() == 1) {
//                    holder.tvGoodsMinus.setVisibility(View.INVISIBLE);
//                    holder.tvGoodsNum.setVisibility(View.INVISIBLE);
                    presenter.removeGood(list.get(position).getId()+"",list.get(position).getProductInfo().getId()+"",position,false);
                }else {
                    presenter.updateCarNum(list.get(position).getId()+"",list.get(position).getCart_num()-1 ,0,position);
                }
//                holder.tvGoodsNum.setText(list.get(position).getCart_num() + "");
            }}
        });
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_shop_goods_name)
        TextView tvShopGoodsName;
        @BindView(R.id.iv_shop_picture)
        ImageView ivShopPicture;

        @BindView(R.id.tv_shop_goods_price)
        TextView tvShopGoodsPrice;
        @BindView(R.id.tv_shop_goods_price_old)
        TextView tvShopGoodsPriceOld;
        @BindView(R.id.tv_goods_add)
        TextView tvGoodsAdd;
        @BindView(R.id.tv_goods_minus)
        TextView tvGoodsMinus;
        @BindView(R.id.tv_goods_num)
        TextView tvGoodsNum;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
